﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using com.huawei.esdk.tp.professional.DataType;

namespace eSDK_TP_Demo_CS_CSharp.FunctionPage
{
    public partial class AddSiteToConf_UI : UserControl
    {
        AuthorizeService loginService = new AuthorizeService();
        ConferenceService service = new ConferenceService();
        List<SiteInfoEx> allSitesInfo = new List<SiteInfoEx>();
        List<SiteStatusEx> siteStatusList = new List<SiteStatusEx>();
        QueryResultCode queryResultCode = new QueryResultCode();

        public AddSiteToConf_UI()
        {
            InitializeComponent();
            getSiteList();
        }

        //获取所有会场列表
        private void getSiteList()
        {
            loginService.Login();
            allSitesInfo = service.QuerySites();
            if (allSitesInfo != null)
                this.SiteInfoList.ItemsSource = allSitesInfo;
            
            loginService.Logout();
        }

        private void AddSiteToConf_Loaded(object sender, RoutedEventArgs e)
        {
            this.beginTimeText.Text = DateTime.Now.ToString();
        }

        //向会议添加会场
        private void AddSiteBut_Click(object sender, RoutedEventArgs e)
        {
            loginService.Login();
            bool falg = true;
            try
            {
                string confId = this.confIDText.Text;
                if (getConfSiteList(confId) == 0)
                {

                    DateTime beginTime = DateTime.Parse(this.beginTimeText.Text);
                    foreach (SiteInfoEx site in this.SiteInfoList.SelectedItems)
                    {
                        falg = true;
                        for (int i = 0; i < siteStatusList.Count; i++)
                        {
                            if (site.uri.Equals(siteStatusList[i].uri))
                            {
                                falg = false;
                                break;
                            }
                        }
                        if (falg)
                        {
                            //调用ConferenceService的AddSiteToConf方法，实现向会议添加会场
                            int result = service.AddSiteToConf(site.name, confId, beginTime, site.type, site.uri);
                            if (result == 0)
                            {
                                this.OperationDefeatText.Visibility = Visibility.Collapsed;
                                this.OperationSucceedText.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                this.OperationDefeatText.Visibility = Visibility.Visible;
                                this.OperationSucceedText.Visibility = Visibility.Collapsed;
                            }
                            this.ResultCodeText.Text = result.ToString();
                        }
                    }
                    getConfSiteList(confId);
                    this.ConfSiteList.ItemsSource = siteStatusList;
                    this.SiteInfoList.Visibility = Visibility.Collapsed;
                    this.ConfSiteList.Visibility = Visibility.Visible;
                }
                else
                {
                    this.OperationSucceedText.Visibility = Visibility.Collapsed;
                    this.OperationDefeatText.Visibility = Visibility.Visible;
                    this.ResultCodeText.Text = queryResultCode.Result.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                loginService.Login();
            }
        }

        private void SiteListBut_Click(object sender, RoutedEventArgs e)
        {
            this.SiteInfoList.Visibility = Visibility.Visible;
            this.ConfSiteList.Visibility = Visibility.Collapsed;
            this.SiteInfoList.ItemsSource = allSitesInfo;
        }

        //获取会议中的会场
        private int getConfSiteList(string confId)
        {
            queryResultCode = service.QueryConfSitesStatus(confId);
            siteStatusList = queryResultCode.SiteStatusList;
            return queryResultCode.Result;
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Line1.X2 = this.GridWidth.ActualWidth;
            this.Line2.X2 = this.GridWidth.ActualWidth;
            this.Line3.X2 = this.GridWidth.ActualWidth;
        }
    }
}
